package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    public void login(String username, String password, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //如果try里面的代码出现了错误，立马进入catch里面捕捉
        try {
            employeeService.login(username,password);
            //如果login方法没有问题我们让他跳转到部门的列表界面
            response.sendRedirect("/department/list");
        }catch (Exception e){
            e.printStackTrace();
            //e.getMessage获取异常信息，账号或密码不正确
            System.out.println(e.getMessage());
            request.setAttribute("errorMsg",e.getMessage());
            //请求转发到login.jsp界面中
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}
